var $;
layui.config({
    base: "js/"
}).use(['form', 'layer', 'jquery', 'laypage'], function () {
    var form = layui.form(),
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        laypage = layui.laypage;
    $ = layui.jquery;

    $.ajax({
        url: "http://localhost/ci/admin/menu/imp_edit",
        type: "POST",
        data: {'menu_id': parent.$('.ci_data').val(), 'token': localStorage.token},
        dataType: "json",
        success: function (data) {
            $('.name').val(data['name']);
            $('.url').val(data['url']);
            $('.perms').val(data['perms']);
            $('.type').val(data['type']);
            $('.icon').val(data['icon']);
            $('.icon').html(data['icon']);
            var obj = document.all.authority;
            for (var i = 0; i < obj.length; i++) {
                console.log(obj[i].id.substring(10));
                console.log(data['menu_id']);
                if (obj[i].id.substring(10) == data['parent_id']) {
                    obj[i].checked = true;
                }
            }
        },
        error: function (data) {
            console.log(data);
        }
    })



    $(".pic").click(function(){

        var index = layui.layer.open({
            title : "选择图片",
            type : 2,
            area: ['600px', '360px'],
            maxmin: true,
            shadeClose: true, //点击遮罩关闭层
            content : "http://localhost/ci/admin/menu/select",
            success : function(layero, index){
                layui.layer.tips('点击此处返回文章列表', '.layui-layer-setwin .layui-layer-close', {
                    tips: 3
                });
            }
        })

    })

    var updateMenu = [];
    form.on("submit(updateMenu)", function (data) {
        var obj = document.all.authority;
        for (var i = 0; i < obj.length; i++) {
            if (obj[i].checked == true) {
                $('.menu_id').val(parseInt(obj[i].id.substring(10)));
            }
        }

        updateMenu = '{"menu_id":"' + parent.$('.ci_data').val() + '","parent_id":"' + $('.menu_id').val() + '","name":"' + $('.name').val() + '","url":"' + $('.url').val() + '","perms":"' + $('.perms').val() + '","type":"' + $('.type').val() + '","icon":"' + $('.icon').html() + '"}';

        console.log(updateMenu);
        $.ajax({
            url: "http://localhost/ci/admin/menu/imp_update",
            type: "post",
            data: {'data': updateMenu, 'token': localStorage.token},
            dataType: "json",
            success: function (data) {
                console.log(data);
                //top.layer.close(index);
                top.layer.msg("用户修改成功！");
                layer.closeAll("iframe");
                //刷新父页面
                parent.location.reload();
            },
            error: function (data) {
                console.log(data);
                //top.layer.close(index);
                top.layer.msg("用户修改失败！");
                layer.closeAll("iframe");
                //刷新父页面
                parent.location.reload();
            }
        })
        return false;
    })

})
